
package com.intel.factorytest.bandtx;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.view.Window;
import android.os.Message;
import android.os.Looper;
import android.os.Handler;
import android.os.SystemProperties;
import android.app.StatusBarManager;
import android.view.KeyEvent;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;

public class BandTestingActivity extends Activity implements View.OnClickListener{

    private final static String TAG = "BandTestingActivity";
    private static final String[] BAND_NAME = 
            new String[] {"LTE TX Band 1", "LTE TX Band 2", "LTE TX Band 3", "LTE TX Band 4", "LTE TX Band 5", "LTE TX Band 7", "LTE TX Band 8",
                          "LTE TX Band 12", "LTE TX Band 13", "LTE TX Band 19", "LTE TX Band 20", "LTE TX Band 21", "LTE TX Band 28",
                          "LTE TX Band 48", "3G TX Band 1", "3G TX Band 2", "3G TX Band 5", "3G TX Band 8"};

    private static final String[] BAND_CMD = 
            new String[] {"lteband01", "lteband02", "lteband03", "lteband04", "lteband05", "lteband07", "lteband08",
                          "lteband12", "lteband13", "lteband19", "lteband20", "lteband21", "lteband28",
                          "lteband48", "3Gband01", "3Gband02", "3Gband05", "3Gband08"};
    
    private final static int BAND_MSG_SET_BUTTON_ENABLE = 1;
    private final static int BAND_MSG_RUN_FINISH = 2;

    private Button mStartButton;
    private Button mStopButton;
    private LinearLayout mButtonView;
    Context mContext;
    private TextView mShowText;
    private TextView mGetMessage;
    ActionBar  actionBar;
    StatusBarManager mStatusBarManager;
    private boolean isShowButton = false;
    private int bandpos = -1;


    protected Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case BAND_MSG_SET_BUTTON_ENABLE: {                    
                    removeMessages(BAND_MSG_SET_BUTTON_ENABLE);
                    mStopButton.setEnabled(true);        
                    break;
                }
                case BAND_MSG_RUN_FINISH: {                    
                    removeMessages(BAND_MSG_RUN_FINISH);
                    finish();
                    break;
                }

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logi( "on create");
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        mContext = getApplicationContext(); 
        //Keep screen on.
        Util.wakeUpAndKeepScreenOn(this);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.band_test_layout);
        mStatusBarManager = (StatusBarManager)getSystemService(STATUS_BAR_SERVICE);        
        //sendDiagCmd("null");
        initView();
    }

    
    public void HideBottomMenu(){
        LogUtils.logi( "HideBottomMenu");
        mStatusBarManager.disable(StatusBarManager.DISABLE_MASK);
        actionBar = getActionBar();
        if(actionBar != null) {
               actionBar.hide();
        }
        View decorView = this.getWindow().getDecorView();        
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        actionBar = getActionBar();
                        if(actionBar != null) {
                            actionBar.hide();
                        }
                        View decorView = getWindow().getDecorView();
                        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_IMMERSIVE
                                | View.SYSTEM_UI_FLAG_FULLSCREEN;
                        decorView.setSystemUiVisibility(uiOptions);
                    }
            });
    }
    
    public void setWindowBrightness(float brightness) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness;
        window.setAttributes(lp);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
           return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                startTX();
                break;
            case R.id.stop:
                stopTX();
                break;
        }
    }
    
    public void startTX() {
        sendDiagCmd(BAND_CMD[bandpos]);
        //sendDiagCmd("ftmin");  // for test
        mStartButton.setEnabled(false);           
        sendDelayedMsg(BAND_MSG_SET_BUTTON_ENABLE,5000,null,null,mHandler);
    }
    
    public void stopTX() {
        if(bandpos >= 0 && bandpos < 14) {
            sendDiagCmd("ltebandstop"); 
        }else if (bandpos >= 14 && bandpos < 18) {
            sendDiagCmd("3Gbandstop"); 
        }else {
            LogUtils.logi( "bandpos error bandpos = " + bandpos);
        }
        mStopButton.setEnabled(false);
        sendDelayedMsg(BAND_MSG_RUN_FINISH,3000,null,null,mHandler);
        //sendDiagCmd("ftmout"); //for test
        //finish();
    }

    private void sendDiagCmd(String bandcmd) {
        SystemProperties.set("persist.sys.chamber.bandtest",bandcmd);
    }

    private int stringPos(String bandname) {        
        LogUtils.logi("bandname = " + bandname + ",BAND_NAME.legth = " + BAND_NAME.length);
        for(int i = 0; i < BAND_NAME.length; i++) {
            if( bandname.equals(BAND_NAME[i]) ) {
               return i;
            }
        }
        return -1;
    }
    protected void initView() {
        LogUtils.logi("initView");
        mShowText = (TextView) findViewById(R.id.showtext);
        mGetMessage = (TextView) findViewById(R.id.get_message);
        mStartButton = (Button) findViewById(R.id.start);
        mStopButton = (Button) findViewById(R.id.stop);
        if(isShowButton) {
            mButtonView = (LinearLayout)findViewById(R.id.bottom);
            mButtonView.setVisibility(View.GONE);
        }
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mStartButton.setEnabled(false);
        mStopButton.setEnabled(false);
    }

    @Override
    protected void onResume() {
        LogUtils.logi( "on onResume");
        super.onResume();
        SystemProperties.set("sys.cit.key.test","true");
        HideBottomMenu();
        mStartButton.setEnabled(true);
        
        Bundle bundle = getIntent().getExtras();
        String bandname = bundle.getString("bandname");        
        LogUtils.logi( "bandname = " + bandname);
        updateMessage(bandname);
        bandpos = stringPos(bandname);
        LogUtils.logi( "bandpos = " + bandpos);
    }

    @Override
    protected void onPause() {
        LogUtils.logi( "onPause");
        SystemProperties.set("sys.cit.key.test","false");        
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        LogUtils.logi( "onDestroy");
        mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE);        
        sendDiagCmd("null");
        super.onDestroy();
    }

    @Override
    public void finish() {
        LogUtils.logi( "finish");
        super.finish();
    }

    public void sendDelayedMsg(int msg, long delayMillis, String putString, String value, Handler mHandler){
        LogUtils.logi( "sendDelayedMsg msg = " + msg + ",delaytime = " + delayMillis + ",putString = " + putString + ",value = " + value);
        if(!TextUtils.isEmpty(putString)&&!TextUtils.isEmpty(value)){
            Bundle b = new Bundle();
            b.putString(putString, value);
            Message m = Message.obtain(mHandler, msg);
            m.setData(b);
            mHandler.sendMessageDelayed(m, delayMillis);
        }else{
            Message m = Message.obtain(mHandler, msg);
            mHandler.sendMessageDelayed(m, delayMillis);
        }
    }
    public void updateTitle(String text) {
        LogUtils.logi( "updateTitle text = " + text);
        mShowText.setText(text);
    }

    public void updateMessage(String text) {        
        LogUtils.logi( "updateMessage text = " + text);
        mGetMessage.setText(text);
    }

}
